/*************************************
DataLogger004

T.Bristow
24/11/12

Reads from 4 connected sensors and sends
the data over the serial port according
to TN001A Prototype dataLogger004 Protocol

*************************************/

#include <stdio.h>

#define BAUD 19200
#define BUFFER 6

unsigned char buffer[BUFFER];
unsigned long* time;
unsigned short* sensors;
long lastMeasure;

void setup(void){
  
  Serial.begin(BAUD);
  
  time=(unsigned long*)buffer;
  sensors=(unsigned short*)(time+1);
 
  // Wait until the initialisation character (0xF0,240) has been received
  while(1){
	if(Serial.available()&&Serial.read()==0xF0) break;
	if(digitalRead(6)==HIGH) break;
  }
  delay(100);
}

void loop(void){
  // wait until at least 1 milliseconds has passed since the last measurement (20 measurements a second)
  while(millis()<lastMeasure+1);
  
  *time=micros();
  
  // Read analog voltages from the sensors 
  *sensors=analogRead(A0);
  
  Serial.write(buffer,BUFFER);
  Serial.flush(); 
  lastMeasure=millis();
}
